package com.dlyk.task;

import com.dlyk.DlykServeApplication;
import com.dlyk.model.TActivity;
import com.dlyk.model.TDicType;
import com.dlyk.model.TDicValue;
import com.dlyk.model.TProduct;
import com.dlyk.result.DicEnum;
import com.dlyk.service.ActivityService;
import com.dlyk.service.DicTypeService;
import com.dlyk.service.ProductService;
import jakarta.annotation.Resource;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @description:
 * @author: ajie
 * @date: 2024/7/26 18:13:11
 * @version: 1.0
 */
@EnableScheduling//开启定时任务
@Component
public class DataTask {

    @Resource
    private DicTypeService dicTypeService;

    @Resource
    private ProductService productService;

    @Resource
    private ActivityService activityService;

    //@Scheduled(cron = "${project.task.cron}",zone = "Asia/Shanghai",timeUnit = TimeUnit.MILLISECONDS)
    @Scheduled(fixedDelayString = "${project.task.delay}",zone = "Asia/Shanghai",timeUnit = TimeUnit.MILLISECONDS)
    public void task(){
        //定时任务开始执行了
        System.out.println("定时任务执行了"+new Date());

        List<TDicType> tDicTypeList = dicTypeService.loadAllDicData();
        DlykServeApplication.cacheMap.put(DicEnum.DICTYPE.getCode(), tDicTypeList);
        tDicTypeList.forEach(tDicType -> {
            String typeCode = tDicType.getTypeCode();
            List<TDicValue> tDicValueList = tDicType.getDicValueList();
            DlykServeApplication.cacheMap.put(typeCode,tDicValueList);
        });

        //查询所有在售产品
        List<TProduct> tProductList = productService.getAllOnSaleProduct();
        DlykServeApplication.cacheMap.put(DicEnum.PRODUCT.getCode(), tProductList);

        //查询所有正在进行的活动
        List<TActivity> tActivityList = activityService.getOngoingActivity();
        DlykServeApplication.cacheMap.put(DicEnum.ACTIVITY.getCode(), tActivityList);


    }
}
